package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)

// UserInfo 用户信息

type UserInfo struct {
gorm.Model
Name string
Gender string
Hobby string
}


func main() {
db, err := gorm.Open("mysql", "root:123456qwe@(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local")
if err!= nil{
panic(err)
}
defer db.Close()

// 自动迁移
db.AutoMigrate(&UserInfo{})

//u1 := UserInfo{Name:"陆杰",Gender: "男",Hobby: "篮球"}
//u2 := UserInfo{Name:"林冲", Gender:"女", Hobby:"足球"}

// 创建记录
//每次增加的时候，会自动将没有补全的字段自动赋值
//db.Create(&u1)
//
//db.Create(&u2)


// 查询

// 获取第一条记录（主键升序）

// SELECT * FROM users ORDER BY id LIMIT 1;
var u = new(UserInfo)
db.First(&u,6)
fmt.Printf("%#v\n", u)
fmt.Println(u.ID)
// 获取最后一条记录（主键降序）
//db.Last(&u)
//fmt.Printf("%#v\n", u)
//获取所有对象 SELECT * FROM users;
uusers:=[]UserInfo{}
find := db.Find(&uusers)
fmt.Println(find.RowsAffected)
for _,i:=range uusers{
fmt.Println(i)
}

//SELECT * FROM users WHERE id IN (1,2,3);
usersin:=[]UserInfo{}
db.Find(&usersin,[]int{1,2,3})

	for _,i:=range usersin{
		fmt.Println(i)
	}



	fmt.Println()
//条件类型
userstiaojian:=[]UserInfo{}
var uu=UserInfo{}
//select * from userinfos where id>2

db.Where("id>?",2).Find(&userstiaojian)
	for _,i:=range userstiaojian{
		fmt.Println(i)
	}
	fmt.Println("结果是")
	db.Where("not name=?","陆杰").First(&uu)
	fmt.Println()
	fmt.Println(uu)




	//struct和map条件
	mapu:=UserInfo{}
	//struct
	db.Where(&UserInfo{Name: "林冲", Hobby: "足球"}).First(&mapu)
	fmt.Println(mapu)
	//map
	mapm:=[]UserInfo{}
	db.Where(&UserInfo{Name:"张三",Hobby:"篮球"}).Find(&mapm)
	for i:=range mapm{
		fmt.Println(mapm[i])
	}

	fmt.Println()
	// 主键切片条件
	mapms:=[]UserInfo{}
	db.Where([]int64{1, 3, 5}).Find(&mapms)
	// SELECT * FROM users WHERE id IN (20, 21, 22);
	for i:=range mapms{
		fmt.Println(mapms[i])
	}


	//如果想要包含零值查询条件，你可以使用 map，其会包含所有 key-value 的查询条件
	//db.Where(map[string]interface{}{"Name": "jinzhu", "Age": 0}).Find(&users)





	//构建 NOT 条件，用法与 Where 类似
	notu:=UserInfo{}
	//notmap:=[]UserInfo{}
	//select * from users where not name=zhangsan
	//db.Not("gender=?","男").Find(&notmap)
	db.Not([]int64{1,2,3}).First(&notu)
	fmt.Println()
	fmt.Println(notu)
	//for i:=range notmap{
	//	fmt.Println(notmap[i])
	//}



 //or条件
 fmt.Println("oroororooorooror")
 oruser:=[]UserInfo{}
 db.Where("not gender=?","男").Or("name=?","张三").Find(&oruser)
 for i:=range oruser{
 	fmt.Println(oruser[i])
 }



 //选择特定字段
 fmt.Println("slelelleellfllflelleelelf")
 seleuser:=[]UserInfo{}
db.Select([]string{"name","gender"}).Find(&seleuser)
for i:=range seleuser{
	fmt.Println(seleuser[i])
}


 //order
 fmt.Println("orderoederodoerodeorodooede")
 orderuser:=[]UserInfo{}
 db.Order(" FIELD(id,1,2,3)").Find(&orderuser)
 for i:=range orderuser{
 	fmt.Println(orderuser[i])
 }


 //limit or offset
 liuser:=[]UserInfo{}
 db.Limit(3).Find(&liuser)
	//db.Offset(3).Find(&users)
	// SELECT * FROM users OFFSET 3;




	//group by & having
	fmt.Println("智能")
	zhineng:=[]moban{}
	db.Model(&UserInfo{}).Limit(5).Find(&moban{})
	for i:=range zhineng{
		fmt.Println(zhineng[i])
	}

}
